/**
 * 
 */
package com.jeecms.wicool.action.question;

import static com.jeecms.common.page.SimplePage.cpn;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.question.Question;
import com.jeecms.wicool.entity.question.QuestionOption;
import com.jeecms.wicool.entity.util.Dictionary2;
import com.jeecms.wicool.manager.question.QuestionMng;
import com.jeecms.wicool.manager.question.QuestionOptionMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

/**
 * @author spring
 *
 */
@Controller
public class QuestionOptionAct {

	@Autowired
	private QuestionOptionMng questionOptionMng;
	
	@Autowired
	private QuestionMng questionMng;
	
	@Autowired
	private DictionaryMng DicMng;
	
	
	@RequestMapping("/questionOption/v_list.do")
	public String list(String title,String questionId,Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = questionOptionMng.getPage(questionId,title,cpn(pageNo), CookieUtils.getPageSize(request));
		
		List<QuestionOption> list = (List<QuestionOption>) pagination.getList();
		
		List<Dictionary2> listQuestionType = DicMng.getList("question_type");
		
		Map<String, String> map = new HashMap<String, String>();
		for (Dictionary2 d : listQuestionType) {
			map.put(d.getDicCode().toString(), d.getDicName());
		}
		
		for (QuestionOption questionOption : list) {
			Question question = questionMng.findById(questionOption.getQuestionId());
			if(question != null){
				questionOption.setQuestionTitle(question.getTitle());
			}
		}
		
		model.addAttribute("pagination", pagination);
		model.addAttribute("title", title);
		model.addAttribute("questionId", questionId);
		model.addAttribute("listQuestion", questionMng.getAll());
		return "question/list_question_option";
	}
	
	@RequestMapping("/questionOption/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		QuestionOption questionOption= new QuestionOption();
		model.addAttribute("questionOption", questionOption);
		model.addAttribute("listQuestion", questionMng.getAll());
		return "question/add_question_option";
	}
	
	@RequestMapping("/questionOption/o_save.do")
	public String save(QuestionOption bean, Integer pageNo,HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = questionOptionMng.save(bean);
		System.out.println("save QuestionOption id="+bean.getId());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/questionOption/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(id, errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		QuestionOption questionOption = questionOptionMng.findById(id);
		model.addAttribute("questionOption", questionOption);
		model.addAttribute("listQuestion", questionMng.getAll());
		return "question/edit_question_option";
	}
	
	@RequestMapping("/questionOption/o_update.do")
	public String update(QuestionOption bean, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(bean.getId(), errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = questionOptionMng.update(bean);
		model.addAttribute("message", "global.success");
		System.out.println("update QuestionOption success. id="+ bean.getId());
		return "redirect:v_list.do";
	}
	
	
	@RequestMapping("/questionOption/o_delete.do")
	public String delete(Integer[] ids, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors =  WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		List<QuestionOption> beans = questionOptionMng.deleteByIds(ids);
		for (QuestionOption bean : beans) {
			System.out.println("delete QuestionOption id=" + bean.getId());
		}
		return "redirect:v_list.do";
	}
	
	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		QuestionOption entity = questionOptionMng.findById(id);
		if (errors.ifNotExist(entity, QuestionOption.class, id)) {
			return true;
		}
		return false;
	}
}
